let QRCode = require('qrcode')

const getElementById = id => document.getElementById(id);

const post = (url, data, cb) => {
    let xhr = new XMLHttpRequest();
    xhr.open('POST', url, false), xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.onreadystatechange = () => {
        if (xhr.readyState == 4) {
            cb(JSON.parse(xhr.responseText), xhr.status, xhr);
        }
    }
    xhr.send(data && JSON.stringify(data));
}
window.create = () => {

    post('/--/create', { url: getElementById('ourl').value }, r => {
        if (r.success) {
            let url = window.location.origin + "/" + r.id;
            getElementById('turl').value = url;

            QRCode.toCanvas(getElementById('qr'), url, function(error) {
                if (error) alert(error);
            });
        } else alert(r.err);
    });

}